
#include <cassert>
#include <chrono>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

void MyTimer(int type, const char *info = nullptr) {
  static std::chrono::steady_clock::time_point start;
  if (type == 0) {
    start = std::chrono::steady_clock::now();
    printf("Begin %s\n", info);
    return;
  } else {
    auto end = std::chrono::steady_clock::now();
    auto dt =
        std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
    printf("End %s, time taken=%lf\n", info, dt.count());
  }
}

/**
 * @brief 开启优化后时间都是一致的
 *
 * @return int
 */

int main() {
  constexpr int nelem = 1000000;

  vector<double> vec(nelem);

  for (int i = 0; i < nelem; ++i) {
    vec[i] = i;
  }

  MyTimer(0, "[]");
  for (int i = 0; i < nelem; ++i) {
    vec[i] += 10;
  }
  MyTimer(1, "");

  MyTimer(0, "iter");
  for (auto i = vec.begin(); i != vec.end(); ++i) {
    (*i) += 10;
  }
  MyTimer(1, "");

  MyTimer(0, "push back");
  vector<int> v1;
  for (int i = 0; i < nelem; ++i)
    v1.push_back(i);
  MyTimer(1, "");

  MyTimer(0, "[]");
  vector<int> v2(nelem);
  for (int i = 0; i < nelem; ++i)
    v2[i] = i;
  MyTimer(1, "");

  return 0;
}